package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsMarketingDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsMarketing;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsMarketing.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsMarketingExample;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryDirectMailGoodsMarketingDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.StringUtil;


@Service
public class DirectMailGoodsMarketingManager {

    @Autowired
    private DirectMailGoodsMarketingDAO dao;

    @Autowired
    private DirectMailGoodsManager directMailGoodsManager;

    private void verify(DirectMailGoodsMarketing record) {
        BizAssert.isTrue(Check.notNull(directMailGoodsManager.getById(record.getDirectMailGoodsId())),
            ResultCode.VERIFY.build(10),
            "直邮商品为空");
        BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getPosition()), ResultCode.VERIFY.build(11), "位置信息为空");
        // 如果是null, 则设置""
        if (Check.isNull(record.getPositionValue())) {
            record.setPositionValue(StringUtil.EMPTY);
        }
    }

    public DirectMailGoodsMarketing add(DirectMailGoodsMarketing record, String createUser) {
        verify(record);
        // 检查商品是否配置过
        BizAssert.isTrue(!exist(record.getPosition(), record.getPositionValue(), record.getDirectMailGoodsId()),
            ResultCode.VERIFY.build(12),
            "直邮商品在当前位置已经配置");

        record.setDeleted(DirectMailGoodsMarketing.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.insert(record);
        return record;
    }

    public DirectMailGoodsMarketing update(DirectMailGoodsMarketing record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.updateByPrimaryKeySelective(record,
            Column.excludes(Column.position,
                Column.positionValue,
                Column.directMailGoodsId,
                Column.deleted,
                Column.createUser,
                Column.createTime));
        return record;
    }

    public void delete(DirectMailGoodsMarketing record, String updateUser) {
//        record.setDeleted(DirectMailGoodsMarketing.IS_DELETED);
//        record.setUpdateUser(updateUser);
//        record.setUpdateTime(Date.from(Instant.now()));
//
//        dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);

        dao.deleteByPrimaryKey(record.getId());
    }

    public boolean exist(String position, String positionValue, Integer directMailGoodsId) {
        DirectMailGoodsMarketingExample example = new DirectMailGoodsMarketingExample();
        DirectMailGoodsMarketingExample.Criteria criteria = example.createCriteria();
        criteria.andPositionEqualTo(position).andDirectMailGoodsIdEqualTo(directMailGoodsId).andDeletedEqualTo(
            DirectMailGoodsMarketing.NOT_DELETED);
        if (Check.notNullOrTrimEmpty(positionValue)) {
            criteria.andPositionValueEqualTo(positionValue);
        }
        return dao.countByExample(example) > 0;
    }

    public DirectMailGoodsMarketing getById(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, DirectMailGoodsMarketing.NOT_DELETED);
    }

    public Pagination<DirectMailGoodsMarketing> query(QueryDirectMailGoodsMarketingDTO query, Sorting[] sorts, Page page) {
        DirectMailGoodsMarketingExample example = new DirectMailGoodsMarketingExample();
        DirectMailGoodsMarketingExample.Criteria criteria = example.createCriteria();

        criteria.andPositionEqualTo(query.getPosition());

        if (Check.notNullOrTrimEmpty(query.getPositionValue())) {
            criteria.andPositionValueEqualTo(query.getPositionValue());
        }
        if (Check.notNull(query.getDirectMailGoodsId())) {
            criteria.andDirectMailGoodsIdEqualTo(query.getDirectMailGoodsId());
        }
        if (Check.notNull(query.getWeight())) {
            criteria.andWeightEqualTo(query.getWeight());
        }

        criteria.andDeletedEqualTo(DirectMailGoodsMarketing.NOT_DELETED);
        example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public long countByDirectMailGoodsId(Integer directMailGoodsId) {
        DirectMailGoodsMarketingExample example = new DirectMailGoodsMarketingExample();
        example.createCriteria().andDirectMailGoodsIdEqualTo(directMailGoodsId).andDeletedEqualTo(
            DirectMailGoodsMarketing.NOT_DELETED);
        return dao.countByExample(example);
    }
}